<?php
namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Controller;
use App\Model\Admin;
use App\Model\MemberTuCao;
use Illuminate\Support\Facades\Input;

/**
 * Class MemberController  员工管理
 * @package App\Http\Controllers\Manage
 */

class MemberController extends Controller{

    protected static $toCao_type = [
        ['id'=>1,'name'=>"行政类建议"],
        ['id'=>2,'name'=>"业务类建议"],
        ['id'=>3,'name'=>"其他建议"],
        ['id'=>4,'name'=>"客户建议"],
    ];
    protected static $toCao_status = [
        ['id'=>1,'name'=>"已解决"],
        ['id'=>2,'name'=>"待解决"],
    ];
    //时间格式字符串
    protected static $format_data = "Y-m-d H:i:s";
    //获取员工吐糟列表
    public function getMemberTuCaoList(){
        $typeid = Input::get('type');
        $statusid = Input::get('status');
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new MemberTuCao();
        if($typeid){
            $obj = $obj->where('type',$typeid);
        }
        if($statusid) {
            $obj = $obj->where('status', $statusid);
        }

        if($orderBy == 1){
            $obj = $obj->orderBy('add_time','ASC');
        }else{
            $obj = $obj->orderBy('add_time','DESC');
        }
        $list = $obj->paginate(10);
        if(empty($page)){
            $count = $obj->count();
        }
        //整理数据
        $type_arr = array();
        foreach (self::$toCao_type as $val){
            $type_arr[$val['id']] = $val['name'];
        }
        $obj_admin = new Admin();
        foreach ($list as &$val){
            if($val['is_anonymous'] == 1){
                $val['mem_nickname'] = "匿名";
            }else{
                $val['mem_nickname'] = $obj_admin->where("id",$val['mem_id'])->pluck("nickname")->first();
            }
            if($val['solve_mem_id']){
                $val['solve_mem_nickname'] = $obj_admin->where("id",$val['solve_mem_id'])->pluck("nickname")->first();
            }else{
                $val['solve_mem_nickname'] = "";
            }
            if($val['status'] == 1){
                $val['status_str'] = "已解决";
            }else{
                $val['status_str'] = "待解决";
            }
            $val['add_time_str'] = date(self::$format_data,$val['add_time']);
            $val['solve_time_str'] = empty($val['solve_time'])?"":date(self::$format_data,$val['solve_time']);
            $val['type_str'] = $type_arr[$val['type']];
        }
        return view("manage.member.memberTuCaoList",
            [
                'list'=>$list,
                'toCaoType'=>self::$toCao_type,
                'toCaoStatus'=>self::$toCao_status,
                'typeid'=>$typeid,
                'statusid'=>$statusid,
                'count'=>$count,
                'orderBy'=>$orderBy,
            ]
        );

    }


    public function upMemberTuCao(){
        $solve_content = Input::get("solve_content");
        $id = Input::get("id");
        $obj = new MemberTuCao();
        $user = session("user");

        $data = [
            'solve_content'=>$solve_content,
            'solve_mem_id'=>$user['id'],
            'status'=>2,
            'solve_time'=>time(),
            'step'=>2,
        ];
        $obj->where("id",$id)->update($data);
        $data_r = [
            'solve_time'=>date(self::$format_data,$data['solve_time']),
            'status'=>"已解决",
            'solve_mem_str'=>Admin::where("id",$user['id'])->pluck("nickname")->first(),
        ];
        return ["error"=>0,"msg"=>"修改成功",'data'=>$data_r];
    }

    function export_csv($filename,$data) {
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $data;
    }

    public function TuExcel(){
        $obj = new MemberTuCao();
        $type_arr = array();
        foreach (self::$toCao_type as $val){
            $type_arr[$val['id']] = $val['name'];
        }
        $obj_admin = new Admin();
        $list = $obj->get();
        $str = "编号,名字,添加时间,类型,描述,建议解决方案,解决方案,解决人,状态,解决时间\n";
        $str = iconv('utf-8','gb2312',$str);
        foreach($list as $row){
            if($row['anonymous'] == 1){
                $row['mem_id'] = "匿名";
            }else{
                $row['mem_id'] = $obj_admin->where("id",$row['mem_id'])->pluck("nickname")->first();
            }
            $men = iconv('utf-8','gb2312',$row["mem_id"]);
            if($row["add_time"]){
                $row["add_time"] = date("Y-m-d",$row["add_time"]);
            }
            $add_time = iconv('utf-8','gb2312',$row["add_time"]);
            $type_str = iconv('utf-8','gb2312',$row["type_str"]);
            $problem_content = iconv('utf-8','gb2312',$row["problem_content"]);
            $advise_solve_content = iconv('utf-8','gb2312',$row["advise_solve_content"]);
            $solve_content = iconv('utf-8','gb2312',$row["solve_content"]);
            if($row["solve_mem_id"]){
                $row['solve_mem_id'] = $obj_admin->where("id",$row['solve_mem_id'])->pluck("nickname")->first();
            }else{
                $row['solve_mem_id'] ="";
            }
            $solve_mem_id = iconv('utf-8','gb2312',$row["solve_mem_id"]);
            if($row["status"]=="1"){
                $row["status"] = "已解决";
            }else{
                $row["status"] = "未解决";
            }
            $status = iconv('utf-8','gb2312',$row["status"]);
            if($row["solve_time"]){
                $row["solve_time"] = date("Y-m-d",$row["solve_time"]);
            }
            $solve_time = iconv('utf-8','gb2312',$row["solve_time"]);
            $str .= $row["id"].",".$men.","."$add_time".","."$type_str".","."$problem_content".","."$advise_solve_content".",".$solve_content.","."$solve_mem_id".","."$status".","."$solve_time"."\n";
        }
        $filename = "吐槽".date("Y-m-d").".csv";
        self::export_csv($filename,$str); //导出
    }
}